<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2022/05/20
//      |_|                                         | TpMeCMS

namespace app\api\controller;

use app\api\controller\Tpmecms;
use think\Db;

/**
 * 粉丝和关注💖
 * 
 */
class Fans extends Tpmecms
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['com'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // /**
    //  * 测试方法
    //  *
    //  * @ApiTitle    (测试名称)
    //  * @ApiSummary  (测试描述信息)
    //  * @ApiMethod   (POST)
    //  * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
    //  * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
    //  * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
    //  * @ApiParams   (name="name", type="string", required=true, description="用户名")
    //  * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
    //  * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
    //  * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
    //  * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
    //  * @ApiReturn   ({
    //      'code':'1',
    //      'msg':'返回成功'
    //     })
    //  */
    // public function test()
    // {
    //     halt(Db::name('area')->where('id', 'between', '1, 5')->select());
    //     $this->success('返回成功', $this->request->param());
    // }

    /**
     * 关注某个用户
     *
     * @param int $uid 用户ID
     * @return void
     */
    public function follow($uid)
    {
        $data['uid'] = $uid;
        $data['fans_uid'] = $this->auth->id;
        if($uid==$this->auth->id){
            $this->error('自己不能关注自己');
        }

        $res = Db::name('fans')->where($data)->find();
        if($res){
            $this->error('你已经关注了该用户');
        }else{
            $data['time'] = time();
            Db::name('fans')->insert($data);
            Db::name('user')->where('id',$uid)->setInc('fans');

            //
            $this->AddMsg($data['uid'],8,$uid,$this->auth->id,$this->auth->nickname.'关注您了');

            $this->success('关注成功');

        }
    }

    
    /**
     * 取消关注某个用户
     *
     * @param int $uid 用户ID
     * @return void
     */
    public function unfollow($uid)
    {
        $data['uid'] = $uid;
        $data['fans_uid'] = $this->auth->id;

        $res = Db::name('fans')->where($data)->find();
        if($res){
            Db::name('fans')->where($data)->delete();
            Db::name('user')->where('id',$uid)->setDec('fans');
            $this->success('取消成功');
           
        }else{         
            $this->error('你未关注该用户',$uid);
        }
    }

    /**
     * 获取我的粉丝列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_my_fans_list($page=1,$limit=10)
    {
        $list = Db::name('fans')->where('uid',$this->auth->id)->order('time desc')->page($page,$limit)->select();
        foreach ($list as $key => &$val) {
            $val['user'] = $this->GetFansUserData($val['fans_uid']);//粉丝信息
            $val['follow'] = $this->GetMyIsfollowUser($val['fans_uid']);//获取我是否关注了某用户
        }

        $this->success('ok',$list);
    }

    /**
     * 获取粉丝信息
     *
     * @param [type] $id 用户id
     * @return void
     */
    protected function GetFansUserData($id)
    {
        return Db::name('user')->where('id',$id)->field('nickname,avatar')->find();
    }


    /**
     * 获取我是否关注了某用户
     *
     * @param [type] $uid 用户id
     * @return void
     */
    protected function GetMyIsfollowUser($uid)
    {
        $res =  Db::name('fans')->where('uid',$uid)->where('fans_uid',$this->auth->id)->find();
        if($res)
            return true;
        return false;

    }


    
    /**
     * 获取对方是否否关注了我
     *
     * @param [type] $uid 对方用户id
     * @return void
     */
    protected function GetUserIsfollowMy($uid)
    {
        $res = Db::name('fans')->where('fans_uid',$uid)->where('uid',$this->auth->id)->find();
        if($res)
            return true;
        return false;
    }


    /**
     * 获取我关注的列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_my_follow_list($page=1,$limit=10)
    {
        $list = Db::name('fans')->where('fans_uid',$this->auth->id)->order('time desc')->page($page,$limit)->select();
        foreach ($list as $key => &$val) {
            $val['user'] = $this->GetFansUserData($val['uid']);//用户信息
            $val['follow'] = $this->GetUserIsfollowMy($val['fans_uid']);//获取对方是否否关注了我
        }

        $this->success('ok',$list);
    }

    /**
     * 获取粉丝排名
     *
     * @param integer $limit 10
     * @return void
     */
    public function get_fans_ranking($limit=10)
    {
        if($limit>100){
            $this->error('传这么大，你想干啥？');
        }
        $list = Db::name('user')->order('fans desc')->field('id,nickname,avatar,fans')->limit($limit)->select();
        foreach ($list as $key => &$val) {
            $val['follow'] = $this->GetMyIsfollowUser($val['id']);
            // unset($val['id']);
        }
        $this->success('ok',$list);
    }



}
 